package itemrecovertool.tasks;

import itemrecovertool.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Matt
 */
public class RecoverTaskGroup extends TaskGroup {
    
    protected List<Item> items = new ArrayList<Item>();
    private final String TEMP_NAME = "temp_ItemRecover";
    private final String SOURCE_FILE = "tasks/ItemRecover";
    private MainFrame mainFrame;
    private boolean changed = false;
    
    public RecoverTaskGroup(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
    }
    
    public boolean changed() {
        return changed;
    }
    
    /** Adds an item to this group, registering it to be recovered. */
    public void addItem(Item item) {
        items.add(item);
        changed = true;
    }
    
    public void removeItem(Item item) {
        boolean b = items.remove(item);
        if (b)
            changed = true;
    }
    
    public void clear() {
        items.clear();
    }
    
    public String getName() {
        return "RecoverGroup";
    }
    
    /** Installs the item recover mod (only if necessary). */
    public boolean install() {
        if (items.size()==0)
            return false;
        
        String modPath = mainFrame.getDataPath()+"\\mods\\";
        
        File source = new File(SOURCE_FILE);
        File dest = new File(modPath+TEMP_NAME);
        try {
            MainFrame.copyDirectory(source, dest);
            
            String spawnPath = modPath+TEMP_NAME+"\\media\\Spawnclasses\\MDITEMRECOVER_TREASURE.dat";
            OutputStreamWriter out = new OutputStreamWriter(
                    new FileOutputStream(spawnPath), "UTF16");
            BufferedWriter bw = new BufferedWriter(out);
            writeln(bw, "[SPAWNCLASS]");
            writeln(bw, "<STRING>NAME:MDITEMRECOVER_TREASURE");
            for (Item i : items) {
                writeln(bw, "[OBJECT]");
                writeln(bw, "<STRING>UNIT:"+i.NAME);
                writeln(bw, "<INTEGER>WEIGHT:-1");
                writeln(bw, "<INTEGER>RARITY_OVERRIDE:1");
                writeln(bw, "[/OBJECT]");
            }
            writeln(bw, "[/SPAWNCLASS]");
            bw.close();
            out.close();
            changed = false;
            return true;
        } catch (IOException e) {
            mainFrame.showError("Recover tasks did not install properly, see errors.log --" +
                    e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
    
    private void writeln(BufferedWriter bw, String line) throws IOException {
        bw.write(line);
        bw.newLine();
    }
    
    public boolean uninstall() {
        File dest = new File(mainFrame.getDataPath()+"\\mods\\"+TEMP_NAME);
        if (dest.exists()) {
            boolean b = MainFrame.deleteDirectory(dest);
            if (b)
                changed = false;
            return b;
        }
        return true;
    }
}
